/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mmt_client;

import java.io.*;
import java.net.*;
import Message.Message;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
/**
 *
 * @author CUCAINHO
 */
public class MMT_CLIENT  {

    /**
     * @param args the command line arguments
     */
    
     public Thread threadRecieve;
     public Thread threadSend;	
    
      
/** Returns an ImageIcon, or null if the path was invalid. */
      public static ImageIcon createImageIcon(String path) {
          java.net.URL imgURL = MMT_CLIENT.class.getResource(path);
          if (imgURL != null) {
              return new ImageIcon(imgURL);
          } else {
              System.err.println("Couldn't find file: " + path);
              return null;
          }
      }
      
    public static void main(String[] args) {
        // TODO code application logic here
        
        try
		{
			
			Socket ss = new Socket("localhost",3211);		
			
			RececieveData(ss);// gọi thread để lúc nào cũng nhận data từ server
			
                        int i = 1;
			while(true)
			{
                        
				//PrintWriter out = new PrintWriter(ss.getOutputStream(),true); 
                               if(i % 5 == 0)
                               {
                                   ImageIcon imgSend = createImageIcon("images/img.jpg");
                                     
                                    System.out.println("Start write."); 
                                    Object obj = new String("abc: " + i);
                                                                        
                                    Message ms = new Message(5,2,imgSend);                               
                                    		

                                    ObjectOutputStream out = new ObjectOutputStream(ss.getOutputStream());                              

                                    out.writeObject(ms);
                                    System.out.println("write thanh cong.");                                
                               }
                               Thread.sleep(1000);
                                i++;
				
			}
		
			
		}
		catch(Exception e)
		{
			System.out.println("Error in fuinction Main!");
                        System.out.println(e.toString());
		}
        
    }
    
    
    public static void RececieveData(Socket ss)
    {
            try	{				
                    ReciveServer sserver = new ReciveServer(ss);					
            }catch(Exception e)		{
                    System.out.println("There're some error");
            }	
    }
    
    
    /*congtt - Functions hand message from server*/
     public static void HandMessage(Message ms , Socket ss) throws IOException{
            
            int type;
            type = ms.GetType();
            switch(type){
                case 1: // chat
                    HandChat(ms);
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    HandRemote(ms , ss);
                    break;
                case 6:
                    break;
                case 7:
                    break;
                case 8:
                    break;
                case 9:
                    break;
                    
            }
            
        }
        
        public static void  HandChat(Message ms){
            String str = ms.GetData().toString();
            System.out.println("Server: " + str);
        }
        
        public static void HandFile(Message ms){
            
        }
        
        public static void HandRemote(Message ms , Socket ss) throws IOException{
            int option = ms.GetOption();
            
            switch(option){
                case 1:
                    System.out.println("server yeu cau view desktop!!!");
                    CreateAndSendDataToServerViewDesktop(ss);
                    break;
                case 2:
                    break;
                default:
                    break;
                            
            }
        }
          
        public static void HandViewDesktop(Message ms){
            
        }
            
        public static void HandShell(Message ms){
            
        }      
         /*end congtt - Functions hand message from server*/
        /*function for send data remote to server */
        
        public static void CreateAndSendDataToServerViewDesktop(Socket ss) throws IOException{
            System.out.println("Zo ham create and send data view sesktop");
            for(int i = 0 ; i < 5 ; i++){
                 System.out.println("Start write image for server.");                 
                ImageIcon imgSend = createImageIcon("images/img.jpg");  
                Message ms = new Message(5,2,imgSend);
                ObjectOutputStream out = new ObjectOutputStream(ss.getOutputStream());                          

                out.writeObject(ms);
                System.out.println("write hinh to server cong.");  
                                    
            }
        }
        
        /*end function for send data remote to server */
}

 class  ReciveServer extends MMT_CLIENT implements Runnable { 
	
	private Socket theclient;  
	private ObjectInputStream in;
	public ReciveServer (Socket theclient) { 
		this.theclient = theclient; 		
		Thread t = new Thread(this); 
		t.start(); 
	} 
	
	public void run() {
		while(true){
		try 
		{      
                        in = new ObjectInputStream(theclient.getInputStream());
                        Message ms = new Message();
                try {
                       ms = (Message)in.readObject(); // doc message                   
                    } catch (Exception ex) {
                        System.out.println("Loi khi doc message." + ex.toString());
                       
                       Logger.getLogger(ReciveServer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                        
                        //while(ms != null) {	
                        if(ms != null) {
                            try
                            {                                    
                                HandMessage(ms , theclient);				
                            }catch(Exception ex){
                                System.out.println("OUT!");

                            }                                    
                              
			} 
                      
                        
		//System.out.println("Close socket in server!");		
		//this.lstClientSocket.remove(theclient);		
		//out.flush();	
		//out.close();
		//in.close(); 	
		//theclient.close(); 		
		} 
		catch (Exception e) { 
                        System.out.println("Client Disconnect!");
                        try {
                            theclient.close();
                        } catch (IOException ex) {
                            Logger.getLogger(ReciveServer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        break;
			//e.printStackTrace(); 
		} 
	} 
        }
	
}